<template>
  <div>
    <div>我是子组件2</div>
    <!--展示子组件1传递的数据-->
    <div>Son1传递的值: {{ count1 }}</div>
    <!--单击按钮，触发父组件传递的修改数据的方法-->
    <button @click="change">传递值给Son1</button>
  </div>
</template>
<script>
import { ref, getCurrentInstance } from 'vue'
export default {
  setup(props, ctx){
    const count1 = ref(0)
    const { appContext } = getCurrentInstance()
    const eventBus = appContext.config.globalProperties.$bus 
    //订阅组件2的事件，等待组件1被触发
    eventBus.on('waitSon1', (val) => {
      console.log('组件1传递来的值', val)
      count1.value = val
    })
    //触发组件1注册的事件
    const change = () => {
      eventBus.emit('waitSon2', 111)
    }
    return { count1, change }
  }
}
</script>